home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / RELEASE.ZIP / sub_arctic / lib / semantic_lens.java < prev    next >
Encoding:
Java Source  |  1996-10-04  |  20.2 KB  |  611 lines

  1.  
  2. package sub_arctic.lib;
  3.  
  4. import sub_arctic.lib.manager;
  5. import sub_arctic.output.drawable;
  6. import sub_arctic.input.pressable;
  7. import sub_arctic.input.move_draggable;
  8. import sub_arctic.input.grow_draggable;
  9. import sub_arctic.input.event;
  10. import sub_arctic.input.pick_collector;
  11. import sub_arctic.input.std_drag_filters;
  12. import java.awt.Point;
  13. import java.awt.Color;
  14. import java.awt.Font;
  15. import java.awt.FontMetrics;
  16.  
  17. /** 
  18.  * Class to implement dragging and drawing for a semantic lens.  When this
  19.  * object is drawn, it does a traversal of the subtree rooted at the parent 
  20.  * of this object.  That traversal does an alternate rendering (semantic 
  21.  * redraw) of the subtree within the bounds of this object.  This redraw 
  22.  * can be done on top of the normal draw (by setting clear_back false, which 
  23.  * is the default), or the normal drawing can be cleared and replaced
  24.  * the alternate rendering.  Alternate renderings are done via a 
  25.  * "interactor_predicate" object that parameterizes this object and is passed
  26.  * to a traverse_and_collect() of the parent.<p>
  27.  * 
  28.  * This object provides a title bar (with an optional text title) that can be
  29.  * used to move the lens, as well as a small grow handle to resize it.  For
  30.  * complete operation, this object should be placed under a 
  31.  * semantic_lens_parent object (the object will work with other parents, but
  32.  * redraw anomalies for dynamically moving objects within the lens can occur).
  33.  *
  34.  * @see semantic_lens_parent
  35.  * @see sem_draw_context
  36.  * @see sem_draw_continue
  37.  * @see base_interactor#traverse_and_collect
  38.  * @author Scott Hudson
  39.  */
  40. public class semantic_lens 
  41.   extends base_interactor implements pressable, move_draggable, grow_draggable {
  42.  
  43.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  44.  
  45.   /** unique identifier for sem_draw traversals */
  46.   protected static final int sem_draw_trav_id = manager.unique_int();
  47.  
  48.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  49.  
  50.   /** 
  51.    * Indication of whether we clear our background first, or draw over the 
  52.    * top of the objects that were drawn before us.
  53.    */
  54.   protected boolean _clear_back = false;
  55.  
  56.   /** 
  57.    * Indication of whether we clear our background first, or draw over the 
  58.    * top of the objects that were drawn before us.
  59.    * @return boolean indicating whether we clear our background.
  60.    */
  61.   public boolean clear_back() {return _clear_back;}
  62.  
  63.   /** 
  64.    * Set whether we clear our background first, or draw over the 
  65.    * top of the objects that were drawn before us.
  66.    * @param boolean v indicating whether we should clear our background.
  67.    */
  68.   public void set_clear_back(boolean v) 
  69.     {
  70.       _clear_back = v;
  71.       damage_self();
  72.      }
  73.  
  74.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  75.  
  76.   /** 
  77.    * The "predicate" object that does the specialized drawing for this 
  78.    * lens.  This object must be expecting a sem_draw_context object for
  79.    * a parameter, and should normally return false in all cases.
  80.    * 
  81.    * @see sem_draw_context
  82.    */
  83.   protected interactor_pred _draw_obj = null;
  84.  
  85.   /** 
  86.    * The "predicate" object that does the specialized drawing for this 
  87.    * lens.  This object must be expecting a sem_draw_context object for
  88.    * a parameter, and should normally return false in all cases.
  89.    *
  90.    * @see sem_draw_context
  91.    * @return interactor_pred the current predicate.
  92.    */
  93.   public interactor_pred draw_obj() {return _draw_obj;}
  94.  
  95.   /** 
  96.    * Set the "predicate" object that does the specialized drawing for this 
  97.    * lens.  This object must be expecting a sem_draw_context object for
  98.    * a parameter, and should normally return false in all cases.
  99.    *
  100.    * @param interactor_pred dobj the drawing "predicate" object.
  101.    */
  102.   public void set_draw_obj(interactor_pred dobj) 
  103.     {
  104.       _draw_obj = dobj;
  105.       damage_self();
  106.     }
  107.  
  108.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  109.  
  110.   /** Title string */
  111.   protected String _title_str = " ";
  112.  
  113.   /** 
  114.    * Title string.
  115.    * @return String the current title string.
  116.    */
  117.   public String title_str() {return _title_str;}
  118.  
  119.   /** 
  120.    * Set title string.
  121.    * @param String str the new title string.
  122.    */
  123.   public void set_title_str(String str) 
  124.     {
  125.       _title_str = str;
  126.       damage_self();
  127.     }
  128.  
  129.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  130.  
  131.   /** Color we do our title handle in. */
  132.   protected Color _title_color = Color.gray;
  133.  
  134.   /** 
  135.    * Color we do our title handle in.
  136.    * @return Color the color.
  137.    */
  138.   public Color title_color() {return _title_color;}
  139.  
  140.   /** 
  141.    * Set color we do our title handle in.
  142.    * @param Color c the new color for out title handle.
  143.    */
  144.   public void set_title_color(Color c) 
  145.     {
  146.       _title_color = c;
  147.       damage_self();
  148.     }
  149.  
  150.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  151.  
  152.   /** Font we draw title handle in */
  153.   protected Font _title_font = new Font("Helvetica",Font.PLAIN,9);
  154.  
  155.   /** 
  156.    * Font we draw title handle in.
  157.    * @return Font the font. 
  158.    */
  159.   public Font title_font() {return _title_font;}
  160.  
  161.   /** 
  162.    * Set font we draw title handle in.
  163.    * @param Font fnt the new font to draw with.
  164.    */
  165.   public void set_title_font(Font fnt) 
  166.     {
  167.       _title_font = fnt;
  168.       metrics = manager.get_metrics(_title_font);
  169.       damage_self();
  170.     }
  171.  
  172.   /** FontMetrics object for the current font */
  173.   protected FontMetrics metrics;
  174.  
  175.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  176.  
  177.   /** 
  178.    * Identifier for the particular kind of semantic redraw pass being done
  179.    * here.  
  180.    */
  181.   protected int _sem_draw_kind = manager.unique_int(); 
  182.  
  183.   /** 
  184.    * Identifier for the particular kind of semantic redraw pass being done
  185.    * here.  
  186.    * @return int a unique identifier for this kind of redraw.
  187.    */
  188.   public int sem_draw_kind() {return _sem_draw_kind;}
  189.  
  190.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  191.  
  192.   /** 
  193.    * Full constructor.  
  194.    * 
  195.    * @param int x             x position of the lens
  196.    * @param int y             y position of the lens
  197.    * @param int w             width of the lens
  198.    * @param int h             height of the lens
  199.    * @param interactor_pred draw_action a "predicate" object which does the
  200.    *                specialized drawing of an object for this lens
  201.    * @param boolean clr_back  do we clear the background before we draw
  202.    * @param String  ttl_str   title string to be drawn on title drag bar
  203.    * @param Color   ttl_color color of title drag bar
  204.    * @param Font    ttl_font  font to draw title in
  205.    * @param int     draw_kind identifier that indicates the kind of semantic
  206.    *                           redraw that we are doing
  207.    */
  208.   public semantic_lens(
  209.     int xv, int yv, 
  210.     int wv, int hv, 
  211.     interactor_pred draw_action, 
  212.     boolean         clr_back,
  213.     String          ttl_str,
  214.     Color           ttl_color,
  215.     Font            ttl_font,
  216.     int             draw_kind) 
  217. {
  218.       super(xv,yv,wv,hv);
  219.       set_draw_obj(draw_action);
  220.       set_clear_back(clr_back);
  221.       if (ttl_color != null) 
  222.     set_title_color(ttl_color);
  223.       if (ttl_font != null) 
  224.     set_title_font(ttl_font);
  225.       else
  226.     set_title_font(_title_font);
  227.       if (ttl_str != null)
  228.         set_title_str(ttl_str);
  229.       _sem_draw_kind = draw_kind;
  230.     }
  231.  
  232.    //had:
  233.    //* @exception general PROPAGATED
  234.  
  235.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  236.  
  237.   /** 
  238.    * Simple constructor with default font and title bar color.
  239.    *  
  240.    * @param int x             x position of the lens
  241.    * @param int y             y position of the lens
  242.    * @param int w             width of the lens
  243.    * @param int h             height of the lens
  244.    * @param interactor_pred   draw_action a "predicate" object which does the
  245.    *                      specialized drawing of an object for this lens
  246.    * @param boolean clr_back  do we clear the background before we draw
  247.    * @param int     draw_kind identifier that indicates the kind of semantic
  248.    *                           redraw that we are doing
  249.    */
  250.   public semantic_lens(
  251.     int xv, int yv, 
  252.     int wv, int hv, 
  253.     interactor_pred draw_action, 
  254.     boolean         clr_back,
  255.     int             draw_kind) 
  256. {
  257.       this (xv,yv,wv,hv,draw_action, clr_back, null, null, null, draw_kind);
  258.     }
  259.  
  260.    //had:
  261.    //* @exception general PROPAGATED
  262.  
  263.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  264.  
  265.   /** Simplest constructor.  Defaults to 0,0 : 100x100 with non-cleared 
  266.    *  background.
  267.    *
  268.    *  @param interactor_pred   draw_action a "predicate" object which does the
  269.    *                   specialized drawing of an object for this lens
  270.    *  @param int     draw_kind identifier that indicates the kind of semantic
  271.    *                           redraw that we are doing
  272.    */
  273.   public semantic_lens(interactor_pred draw_action, int draw_kind) 
  274. {
  275.       this(0,0,100,100,draw_action,false,draw_kind);
  276.     }
  277.  
  278.    //had:
  279.    //* @exception general PROPAGATED
  280.  
  281.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  282.  
  283.   /** 
  284.    * Override pick to just work on handles and let center of lens pass through.
  285.    *  
  286.    * @param int pt_x       x coordinate of picking point
  287.    * @param int pt_y       y coordinate of picking point
  288.    * @param pick_collector pick_list result list
  289.    */
  290.   public void pick(int pt_x, int pt_y, pick_collector pick_list) 
  291. {
  292.       int title_h;
  293.  
  294.       /* if it doesn't hit our bound we're done */
  295.       if (!inside_bounds(pt_x, pt_y)) return;
  296.  
  297.       /* see if it hits title handle */
  298.       title_h = metrics.getHeight()+2;
  299.       if (pt_y < title_h)
  300.     {
  301.       pick_list.report_pick(this, new Integer(0));
  302.       return;
  303.     }
  304.       
  305.       /* see if it hits the size handle */
  306.       if (pt_x >= w()-10 && pt_y >= h()-10)
  307.     {
  308.       pick_list.report_pick(this, new Integer(1));
  309.       return;
  310.     }
  311.     }
  312.  
  313.    //had:
  314.    //* @exception general PROPAGATED
  315.  
  316.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  317.  
  318.   /** 
  319.    * Handle mouse button press input to the object by making us either the
  320.    * move-drag or grow-drag focus.
  321.    *
  322.    * @param event  evt       the press event.
  323.    * @param Object user_info the information associated with this object 
  324.    *                         at pick time.
  325.    * @return boolean indicating whether the event was consumed.
  326.    */
  327.   public boolean press(event evt, Object user_info)
  328.     {
  329.       int which = ((Integer)user_info).intValue();
  330.       if (which == 0) // title bar
  331.     {
  332.       /* make us the move-drag focus */
  333.       manager.move_drag_focus.set_focus_to(this, evt, null);
  334.      return true;
  335.     }
  336.       else if (which == 1) // grow handle
  337.     {
  338.       /* make us the grow-drag focus */
  339.       manager.grow_drag_focus.set_focus_to(this, evt, null);
  340.      return true;
  341.     }
  342.  
  343.       /* shouldn't happen, but we have to keep the compiler happy */
  344.       return false;
  345.     }
  346.  
  347.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  348.  
  349.   /** 
  350.    * Companion to press -- here we ignore the releases. 
  351.    * @param event  evt       the release event.
  352.    * @param Object user_info information associated with this object at 
  353.    *                         pick time.
  354.    * @return boolean indicating whether we consumed this input (here always 
  355.    *                 false).
  356.    */
  357.   public boolean release(event evt, Object user_info)
  358.     {
  359.      return false;
  360.     }
  361.  
  362.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  363.  
  364.   /** 
  365.    * Handle the start of a move-drag to the object.  
  366.    *
  367.    * @param event  evt       the event "causing" the drag.
  368.    * @param int    xv        the x location of the object at start of drag (in 
  369.    *                         parent's coordinates).
  370.    * @param int    yv        the y location of the object at start of drag (in 
  371.    *                         parent's coordinates).
  372.    * @param int    gx        initial grab location within the object.
  373.    * @param int    gy        initial grab location within the object.
  374.    * @param Object user_info the information associated with this object when 
  375.    *                         it requested drag focus.
  376.    * @return boolean indicating whether the input was consumed. 
  377.    */
  378.   public boolean drag_start(
  379.     event evt, int xv, int yv, int gx, int gy, Object user_info)
  380.     {
  381.       /* move our position */
  382.       set_pos(xv,yv);
  383.       return true;
  384.     }
  385.  
  386.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  387.  
  388.   /** 
  389.    * Handle a movement during a move-drag.  Here we just set our position to 
  390.    * follow the event location. 
  391.    *
  392.    * @param event  evt       the event "causing" the drag.
  393.    * @param int    x_pos     the x position this object should move to.
  394.    * @param int    y_pos     the y position this object should move to.
  395.    * @param int    start_x   the x location of the object at start of drag (in 
  396.    *                         parent's coordinates).
  397.    * @param int    start_x   the y location of the object at start of drag (in 
  398.    *                         parent's coordinates).
  399.    * @param int    grab_x    initial grab location within the object.
  400.    * @param int    grab_y    initial grab location within the object.
  401.    * @param Object user_info the information associated with this object when 
  402.    *                         it requested drag focus.
  403.    * @return boolean indicating whether the input was consumed. 
  404.    */
  405.   public boolean drag_feedback(
  406.     event evt,
  407.     int x_pos, int y_pos,
  408.     int start_x, int start_y,
  409.     int grab_x, int grab_y,
  410.     Object user_info)
  411.     {
  412.  
  413.       /* move our position */
  414.       set_pos(x_pos, y_pos);
  415.       return true;
  416.     }
  417.  
  418.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  419.  
  420.   /** 
  421.    * Handle input corresponding to the end of a move-drag.  In addition to 
  422.    * normal dragging, we force the object back inside its parent if it is
  423.    * too far outside.
  424.    *
  425.    * @param event  evt       the event "causing" the drag.
  426.    * @param int    x_pos     the x position this object should move to.
  427.    * @param int    y_pos     the y position this object should move to.
  428.    * @param int    start_x   the x location of the object at start of drag (in 
  429.    *                         parent's coordinates).
  430.    * @param int    start_x   the y location of the object at start of drag (in 
  431.    *                         parent's coordinates).
  432.    * @param int    grab_x    initial grab location within the object.
  433.    * @param int    grab_y    initial grab location within the object.
  434.    * @param Object user_info the information associated with this object when 
  435.    *                         it requested drag focus.
  436.    * @return boolean indicating whether the input was consumed. 
  437.    */
  438.   public boolean drag_end(
  439.     event evt,
  440.     int x_pos, int y_pos,
  441.     int start_x, int start_y,
  442.     int grab_x, int grab_y,
  443.     Object user_info)
  444.     {
  445.       boolean result;
  446.  
  447.       /* let drag_feedback to all the drag work */
  448.       result=drag_feedback(evt, x_pos, y_pos, start_x, start_y, grab_x, grab_y,
  449.                                     user_info);
  450.  
  451.       /* force us back inside our parent if we ended up out */
  452.       if (x() < 10-w()) set_x(10-w());
  453.       if (y() < 0)      set_y(0);
  454.       if (x() > parent().w()-10) set_x(parent().w()-10);
  455.       if (y() > parent().h()-10) set_y(parent().h()-10);
  456.  
  457.       return result;
  458.     }
  459.  
  460.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  461.  
  462.   /** 
  463.    * Handle the start of a grow-drag to the object.  
  464.    *
  465.    * @param event  evt       the event "causing" the drag.
  466.    * @param Object user_info the information associated with this object at 
  467.    *                         the point that it requested focus.
  468.    * @return boolean indicating whether the input was consumed. 
  469.    */
  470.   public boolean drag_start(event evt, Object user_info)
  471.     {
  472.       /* nothing to do here */
  473.       return true;
  474.     }
  475.  
  476.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  477.  
  478.   /** 
  479.    * Handle a movement during a grow-drag.  Here we just set our size to 
  480.    * follow the drag, but don't go smaller than 30x30. 
  481.    *
  482.    * @param event  evt       the event "causing" the drag.
  483.    * @param int    cur_w     the current width indicated by the drag.
  484.    * @param int    cur_h     the current height indicated by the drag.
  485.    * @param int    st_w      the starting width of this object.
  486.    * @param int    st_w      the starting height of this object.
  487.    * @param Object user_info the information associated with this object at 
  488.    *                         the point that it requested focus.
  489.    * @return boolean indicating whether the input was consumed. 
  490.    */
  491.   public boolean drag_feedback(
  492.     event evt, 
  493.     int cur_w, int cur_h,
  494.     int st_w, int st_h, 
  495.     Object user_info)
  496.     {
  497.       /* don't go smaller than 30x30 */
  498.       if (cur_w < 30) cur_w = 30;
  499.       if (cur_h < 30) cur_h = 30;
  500.  
  501.     /* set our size */
  502.       set_size(cur_w, cur_h);
  503.       return true;
  504.     }
  505.  
  506.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  507.  
  508.   /** 
  509.    * Handle input corresponding to the end of a grow-drag.  
  510.    *
  511.    * @param event  evt       the event "causing" the drag.
  512.    * @param int    cur_w     the current width indicated by the drag.
  513.    * @param int    cur_h     the current height indicated by the drag.
  514.    * @param int    st_w      the starting width of this object.
  515.    * @param int    st_w      the starting height of this object.
  516.    * @param Object user_info the information associated with this object at 
  517.    *                         the point that it requested focus.
  518.    * @return boolean indicating whether the input was consumed. 
  519.    */
  520.   public boolean drag_end(
  521.     event evt, 
  522.     int cur_w, int cur_h, 
  523.     int st_w,  int st_h, 
  524.     Object user_info)
  525.     {
  526.       /* let drag_feedback to all the work */
  527.       return drag_feedback(evt, cur_w, cur_h, st_w, st_h, user_info);
  528.     }
  529.  
  530.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  531.  
  532.   /** 
  533.    * Override draw_self to do a semantic lens.  This works by going up one
  534.    * level to the parent, then doing a sem_draw traversal from there down.
  535.    * the sem_draw traversal draws a semantically modified rendition of the
  536.    * sub-tree on our drawable.<p>
  537.    *
  538.    * Note, this drawing is done inside our bounds, but the drawing 
  539.    * corresponding to each individual object found in the traversal starting
  540.    * from our parent is not limited to their bounds.  This requires special
  541.    * processing in damage_self within our parent.
  542.    *
  543.    * @see semantic_draw_parent
  544.    * @see sem_draw_context
  545.    * @see sem_draw_continue
  546.    * @see sem_draw_to_child
  547.    * @see base_interactor#traverse_and_collect
  548.    * @param drawable d the drawing surface we draw on.
  549.    */
  550.   protected void draw_self_local(drawable d) 
  551. {
  552.       drawable         dcopy;
  553.       sem_draw_context context;
  554.       int              hv;
  555.  
  556.       /* clear our background if needed */
  557.       if (clear_back())
  558.     {
  559.       d.setColor(Color.white);
  560.       d.fillRect(0,0,w()-1, h()-1);
  561.     }
  562.       d.setColor(Color.black);
  563.  
  564.       /* make a copy of our drawable and put it in our parent's coords */
  565.       dcopy = d.copy();
  566.       dcopy.translate(-x(), -y());
  567.  
  568.       /* make a sem_draw_context out of that */
  569.       context = new sem_draw_context(sem_draw_kind(), dcopy);
  570.  
  571.       /* do the redraw traversal starting at our parent */
  572.       parent().traverse_and_collect(sem_draw_trav_id, TRAV_DRAW, draw_obj(),
  573.     new sem_draw_continue(), new sem_draw_to_child(), context, 
  574.     new pick_collector());
  575.       d.drawRect(0,0,w()-1, h()-1);
  576.  
  577.       /* draw our title bar handle */
  578.       hv = metrics.getHeight()+4;
  579.       d.setColor(title_color());
  580.       d.fillRect(0,0, w()-1, hv);
  581.       d.setColor(Color.white);
  582.       d.setFont(title_font());
  583.       d.drawString(title_str(), 2, metrics.getAscent()+2);
  584.  
  585.       /* draw our grow handle */
  586.       d.setColor(title_color());
  587.       d.fillRect(w()-10, h()-10, 10,10);
  588.  
  589.       /* draw a rectangle over the top */
  590.       d.setColor(Color.black);
  591.     }
  592.  
  593.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  594. }
  595. /*=========================== COPYRIGHT NOTICE ===========================
  596.  
  597. This file is part of the subArctic user interface toolkit.
  598.  
  599. Copyright (c) 1996 Scott Hudson and Ian Smith
  600. All rights reserved.
  601.  
  602. The subArctic system is freely available for most uses under the terms
  603. and conditions described in 
  604.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  605. and appearing in full in the lib/interactor.java source file.
  606.  
  607. The current release and additional information about this software can be 
  608. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  609.  
  610. ========================================================================*/
  611.